package the.guanzhong.delivery.interfaces.controller;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.util.DateUtils;
import org.wisteria.framework.web.APIResult;
import the.guanzhong.delivery.interfaces.controller.req.TaskListReq;
import the.guanzhong.delivery.interfaces.controller.req.TaskReceiveReq;
import the.guanzhong.delivery.interfaces.controller.req.TaskTakeReq;
import the.guanzhong.delivery.interfaces.controller.resp.TaskItemResp;
import the.guanzhong.delivery.service.DeliveryTaskService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 配送任务控制器
 */
@Api(tags = "Delivery")
@RestController
@RequestMapping("/delivery/task")
public class DeliveryTaskController {

    @Resource
    private DeliveryTaskService deliveryTaskService;

    /**
     * 查询配送任务列表
     * @param req   查询条件
     * @return
     */
    @PostMapping("/list")
    public APIResult<List<TaskItemResp>> list(@RequestBody @Valid TaskListReq req) {
        return APIResult.success(deliveryTaskService.list(req));
    }

    /**
     * 接单
     */
    @PostMapping("/receive")
    public APIResult<Boolean> receive(@RequestBody @Valid TaskReceiveReq req) {
        return APIResult.success(deliveryTaskService.receive(req));
    }

    /**
     * 取件
     */
    @PostMapping("/take")
    public APIResult<Boolean> take(@RequestBody @Valid TaskTakeReq req) {
        return APIResult.success(deliveryTaskService.take(req));
    }

    @Resource
    private ApiHelper pushApiHelper;

    /**
     * 推送配送消息
     * @return
     */
    @PostMapping("/push")
    public APIResult<Boolean> push() {
        GTNotification notification = new GTNotification();
        notification.setClickType("startapp");
        notification.setTitle("您有一个新订单,请及时接单");
        notification.setBody("取件地址:许村及周边加工厂 3幢;下单时间:2023-07-07 12:13:52");

        PushMessage pushMessage = new PushMessage();
        pushMessage.setNetworkType(0);
        pushMessage.setNotification(notification);

        PushApi pushApi = pushApiHelper.creatApi(PushApi.class);
        PushDTO pushDTO = new PushDTO();
        pushDTO.setRequestId(System.nanoTime() + "");
        pushDTO.setPushMessage(pushMessage);

        ApiResult apiResult = pushApi.pushAll(pushDTO);
        return APIResult.success(true);
    }
}
